<template>
  <div class="popup-wrap" @click.stop="">
    <div class="popup-inner">
      <div class="popup-top" v-if="conf.title">
        <div class="main-title">{{conf.title}}</div>
        <div class="sub-title" v-if="conf.subTitle" :title="conf.subTitle">{{conf.subTitle}}</div>
        <slot name="top"/>
        <div class="btn-group">
          <span v-for="(btn, index) in conf.topBtns" :key="index" @click="onFootBtnClick(btn)">{{btn.name}}</span>
          <span class="close-btn" @click="hide" v-show="!conf.noCloseBtn"></span>
        </div>
      </div>
      <div><slot/></div>
      <div class="popup-foot">
        <div><slot name="foot"/></div>
        <div>
          <span v-for="(btn, index) in conf.footBtn" @click="onFootBtnClick(btn)" :key="index">{{btn.name}}</span>
          <span @click="onCancle" v-if="conf.onCancle">取消</span>
          <span @click="onConfirm" v-if="conf.onConfirm">确认</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
/**
 * conf.title 主标题
 * conf.subTitle 副标题
 * conf.noCloseBtn 不需要右上角的关闭按钮
 * conf.topBtns[Btn] 头部的按钮组
 * conf.footBtn[Btn] 底部的按钮组
 *  Btn.name 按钮名称
 *  Btn.click 按钮点击回调
 * onConfirm 确认按钮 （最后一个， 默认标题为 确认， 如果需要修改 建议用 conf.footBtn）
 * onCancle 取消按钮（导数第二个， 默认标题为 取消， 如果需要修改 建议用 conf.footBtn）
 * solt : 内容区域
 * slot.top 头部中间
 * slot.foot 底部左侧
 */
export default {
  name: 'popup',
  props: ["conf"],
  data() {return {}},
  computed: {},
  mounted() {},
  methods: {
    hide(){this.conf.hide();},
    onFootBtnClick(btn){
      btn.click && btn.click(
        () => this.hide(),
        this.conf
      );
    },
    onCancle(){
      this.conf.onCancle && this.conf.onCancle(
        () => this.hide(),
        this.conf
      );
    },
    onConfirm(){
      this.conf.onConfirm && this.conf.onConfirm(
        () => this.hide(),
        this.conf
      );
    },
  },
  watch: {},
}
</script>

<style scoped>
  .popup-wrap{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
  .popup-inner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 700px;
    padding-top: 10px;
    background-color: #fff;
    border-radius: 4px;
  }
  .popup-top{
    padding: 10px;
    display: flex;
    border-bottom: 1px solid #eee;
  }
  .main-title{
    flex: 0 0 auto;
    font-size: 16px;
    color: #666;
    padding-right: 10px;
    line-height: 16px;
  }
  .sub-title{
    flex: 1 1 auto;
    font-size: 12px;
    color: #666;
    line-height: 16px;
    padding-left: 10px;
    border-left: 4px solid var(--cl-major);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .popup-top *{
    vertical-align: middle;
  }
  .btn-group{
    text-align: right;
    flex: 1 1 0;
    line-height: 16px;
    white-space: nowrap;
  }
  .btn-group>span:not(:last-child){
    padding: 0 10px;
    opacity: 1;
    cursor: pointer;
    transition: all .3s;
  }
  .btn-group>span:not(:last-child):hover{
    color: red;
  }
  .btn-group>span:not(:last-child):active{
    opacity: .5;
  }
  .close-btn{
    display: inline-block;
    width: 16px;
    height: 16px;
    position: relative;
    cursor: pointer;
  }
  .close-btn:before,
  .close-btn:after{
    position: absolute;
    content: "";
    display: block;
    background-color: red;
    width: 4px;
    height: 100%;
    top: 50%;
    left: 50%;
    border-radius: 2px;
    transform: translate(-50%, -50%);
    opacity: 1;
    transition: all .3s;
  }
  .close-btn:after{
    transform: translate(-50%, -50%) rotate(45deg);
  }
  .close-btn:before{
    transform: translate(-50%, -50%) rotate(-45deg);
  }
  .close-btn:hover:after{
    transform: translate(-50%, -50%) rotate(135deg);
  }
  .close-btn:hover:before{
    transform: translate(-50%, -50%) rotate(45deg);
  }
  .close-btn:active:before,
  .close-btn:active:after{
    opacity: .5;
  }
  .popup-foot{
    border-top: 1px solid #eee;
    padding: 10px;
    display: flex;
    line-height: 24px;
    font-size: 12px;
  }
  .popup-foot>*:first-child{
    flex: 1 1 0;
  }
  .popup-foot>*:last-child{
    text-align: center;
  }
  .popup-foot>*:last-child>span{
    display: inline-block;
    padding: 0 10px;
    opacity: 1;
    cursor: pointer;
    transition: all .3s;
  }
  .popup-foot>*:last-child>span:hover{
    color: red;
  }
  .popup-foot>*:last-child>span:active{
    opacity: .5;
  }
  .popup-foot>*:last-child>span:last-child{
    color: #fff;
    background-color: red;
    border-radius: 4px;
  }
  .popup-foot>*:last-child>span:last-child:hover{
    opacity: .8;
  }
  .popup-foot>*:last-child>span:last-child:active{
    opacity: .5;
  }
</style>
